package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "SELECT p.*,c.name cuisine_name FROM product p ,cuisine  c\n" +
                "where p.cuisine_id=c.id order by id desc";
        return getList(sql);
    }

    @Override
    public ProductModel findById(ProductModel productModel) {
        String sql = "SELECT p.*,c.name cuisine_name FROM product p ,cuisine  c" +
                " where p.id=" + productModel.getId();
        return getList(sql).get(0);
    }

    private List<ProductModel> getList(String sql) {
        List<ProductModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getFloat("normal_price"));
                model.setPrice(resultSet.getFloat("price"));
                model.setContent(resultSet.getString("content"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setIsRecommend(resultSet.getInt("is_recommend"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
                model.setCuisineName(resultSet.getString("cuisine_name"));
                model.setCreateTime(resultSet.getString("create_time"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list.size() == 0) {
            list.add(null);
        }
        return list;
    }

    @Override
    public Integer insert(ProductModel productModel) {
        String sql = "INSERT INTO `product` " +
                "(`name`, `normal_price`, `price`, " +
                "`content`, `is_show`, `is_recommend`, " +
                "`img_href`, `cuisine_id`, " +
                "`create_time`, `update_time`, " +
                "`remark`, `enable`, `where_show`) VALUES " +
                "('" + productModel.getName() + "', '" + productModel.getNormalPrice() + "', '" + productModel.getPrice() + "'," +
                " '" + productModel.getContent() + "', '" + productModel.getIsShow() + "', '" + productModel.getIsRecommend() + "'," +
                " '" + productModel.getImgHref() + "', '" + productModel.getCuisineId() + "', " +
                "now(), now()," +
                " '', '1', '3');\n";
        return JDBC.update(sql);
    }

    @Override
    public Integer update(ProductModel productModel) {
        String sql = "UPDATE `product` SET ";
        if ("".equals(productModel.getName())) {
            sql += "`name` = '" + productModel.getName() + "',";
        }
        if (productModel.getNormalPrice() == 0.0f) {
            sql += " `normal_price` = '" + productModel.getNormalPrice() + "',";
        }
        if (productModel.getPrice() == 0.0f) {
            sql += " `price` = '" + productModel.getPrice() + "', ";
        }
        if ("".equals(productModel.getContent())) {
            sql += "`content` = '" + productModel.getContent() + "', ";
        }
        if (productModel.getIsShow() == 0) {
            sql += "`is_show` = '" + productModel.getIsShow() + "', ";
        }
        if (productModel.getIsRecommend() == 0) {
            sql += "`is_recommend` = '" + productModel.getIsRecommend() + "', ";
        }
        if ("".equals(productModel.getImgHref())) {
            sql += "`img_href` = '" + productModel.getImgHref() + "', ";
        }
        sql += "`cuisine_id` = '" + productModel.getCuisineId() + "'," +
                " `update_time` = now()," +
                " `remark` = ''," +
                " `enable` = '" + productModel.getEnable() + "' " +
                "WHERE (`id` = '" + productModel.getId() + "');\n";
        return JDBC.update(sql);
    }

    @Override
    public Integer delete(ProductModel productModel) {
        String sql = "delete from product where id=" + productModel.getId();
        return JDBC.update(sql);
    }
}
